package tianwang.test;

import java.io.File;
import java.io.IOException;
import java.util.Collection;

import tianwang.dao.IBatisDao;
import tianwang.model.TDModel;
import tianwang.services.typeddependencies.TypedDependenciesStanford;
import tianwang.util.FileUtil;
import tianwang.utils.punctuator.PunctuatorStanfordImpl;
import edu.stanford.nlp.trees.TypedDependency;

public class Haha {
	
	private IBatisDao dao = null;
	private PunctuatorStanfordImpl pun = null;
	public Haha() throws IOException{
		dao =new IBatisDao();
		this.pun = new PunctuatorStanfordImpl();
	}
	public static void main(String[] a) throws Exception{
		Haha main = new Haha();
		
//		File folderFiles = new File("D://测试语料//magazine");
//		File folderFiles = new File("D://测试语料//VOA");
		File folderFiles = new File("D://测试语料//news");
//		File folderFiles = new File("D://测试语料//ncetext//nce2");
//		File folderFiles = new File("D://测试语料//ncetext//nce3");
//		File folderFiles = new File("D://测试语料//ncetext//nce4");
		
		
		if(folderFiles.isDirectory()){
			File[] files = folderFiles.listFiles();
			
			
			for(int f = 0, fLen = files.length; f< fLen; f++){
				main.dealEachFile(files[f]);
			}
			
			System.out.println("计算完成！");
		}
		else{
			System.err.println("in文件夹可能不存在！");
		}
		
	
		
		
	}
	
	public void dealEachFile(File file) throws Exception{
		if(file.isDirectory()){
			File[] subfiles = file.listFiles();
			for(File subFile :subfiles){
				dealEachFile(subFile);
			}
		}
		
		
		
		System.out.println("正在处理" +file.getName());
		
		String content = FileUtil.getContent(file.getAbsolutePath());
		String[] sents = this.pun.punctuate(content);
		
		for(String str: sents){
			TypedDependenciesStanford tds = new TypedDependenciesStanford();
			Collection<TypedDependency> tdCollection = tds.typedDependencies(str);
			IBatisDao dao = new IBatisDao();
			for(TypedDependency td : tdCollection){
				TDModel tdModel = new TDModel(td.toString());
				tdModel.setFrom(file.getName());
				tdModel.setContext(str);
				dao.insert(tdModel);
			}
		}
		
	}
		
}
